package com.ruoyi.course.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.course.vo.ExamCourseTagRelationVo;
import com.ruoyi.course.vo.ExamCourseVo;
import com.ruoyi.course.vo.ExamCourseBankDetailVo;
import com.ruoyi.course.service.IExamCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 课程信息Controller
 *
 * @author Tellsea
 * @date 2025-09-25
 */
@Api(tags = "课程信息Controller")
@RestController
@RequestMapping("/course/course")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ExamCourseController extends BaseController {

    private final IExamCourseService examCourseService;

    @ApiOperation("查询课程信息列表")
    @PreAuthorize("@ss.hasPermi('course:course:list')")
    @GetMapping("/list")
    public TableDataInfo<ExamCourseVo> list(ExamCourseVo entity) {
        return examCourseService.queryList(entity);
    }

    @ApiOperation("查询课程信息所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(ExamCourseVo entity) {
        return AjaxResult.success("查询成功", examCourseService.queryAll(entity));
    }

    @ApiOperation("导出课程信息列表")
    @PreAuthorize("@ss.hasPermi('course:course:export')")
    @Log(title = "课程信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamCourseVo entity) {
        List<ExamCourseVo> list = examCourseService.queryAll(entity);
        ExcelUtil<ExamCourseVo> util = new ExcelUtil<>(ExamCourseVo.class);
        util.exportExcel(response, list, "课程信息数据");
    }

    @ApiOperation("获取课程信息详细信息")
    @PreAuthorize("@ss.hasPermi('course:course:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", examCourseService.queryById(id));
    }

    @ApiOperation("新增课程信息")
    @PreAuthorize("@ss.hasPermi('course:course:add')")
    @Log(title = "课程信息", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody ExamCourseVo entity) {
        boolean isSuccess = examCourseService.save(entity);
        entity.setCreateTime(DateUtils.getNowDate());
        entity.setCreateBy(getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        entity.setUpdateBy(getUsername());
        if(isSuccess){
            return AjaxResult.success(entity.getId());
        }else{
            return AjaxResult.error("新增失败");
        }
        
    }

    @ApiOperation("修改课程信息")
    @PreAuthorize("@ss.hasPermi('course:course:edit')")
    @Log(title = "课程信息", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody ExamCourseVo entity) {
        return toAjax(examCourseService.updateById(entity));
    }

    @ApiOperation("删除课程信息")
    @PreAuthorize("@ss.hasPermi('course:course:remove')")
    @Log(title = "课程信息", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(examCourseService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    @ApiOperation("一对多批量新增课程标签")
    @PostMapping("/addTagRelation")
    public AjaxResult addTagRelation(@RequestBody ExamCourseTagRelationVo entity) {
        return toAjax(examCourseService.saveTagRelation(entity));
    }

    @ApiOperation("查询课程题库列表")
    @GetMapping("/banks/{courseId}")
    public TableDataInfo<ExamCourseBankDetailVo> getCourseBanks(@PathVariable("courseId") Long courseId, ExamCourseBankDetailVo entity) {
        entity.setCourseId(courseId);
        return examCourseService.queryCourseBankList(entity);
    }

    @ApiOperation("新增课程题库关系")
    @Log(title = "新增课程题库关系", businessType = BusinessType.INSERT)
    @PostMapping("/addBank/{courseId}")
    public AjaxResult addCourseBank(@PathVariable("courseId") Long courseId, @RequestBody List<Long> bankIds) {
        if (bankIds == null || bankIds.isEmpty()) {
            return AjaxResult.error("题库ID列表不能为空");
        }
        int successCount = examCourseService.addCourseBanks(courseId, bankIds);
        return AjaxResult.success("成功添加 " + successCount + " 个题库关系", successCount);
    }

    @ApiOperation("移除课程题库关系")
    @Log(title = "移除课程题库关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeBank/{courseId}/{bankId}")
    public AjaxResult removeCourseBank(@PathVariable("courseId") Long courseId, @PathVariable("bankId") Long bankId) {
        return toAjax(examCourseService.removeCourseBank(courseId, bankId));
    }
}
